home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / MWSTAR.ZIP / README.TXT < prev    next >
Encoding:
Text File  |  1996-09-07  |  8.8 KB  |  185 lines

  1.                                                 by Matthew Wilhelm
  2.  
  3.                            Tiny Parallax Starfield
  4.  
  5. First:
  6.         STARNOKB.COM DOES NOT CHECK FOR A KEYSTROKE TO EXIT. IT IS AN
  7. INFINITE LOOP. UNLESS YOU RUN IT UNDER A DOS SHELL (OS/2, WINDOWS 95,
  8. ETC) YOU WILL NEED TO REBOOT YOUR MACHINE.
  9.  
  10.  
  11. Description:
  12.  
  13.         These files are a modification of my winning entry to the small,
  14. randomized ~200 star 4+ speed parallax scroller speed competition. The
  15. code delivers multicolor stars at many different scroll rates, exceeding
  16. the specifications of the competition.
  17.  
  18.         It runs tolerably on my 486/50 with ET4000 ISA video card.
  19.         It runs quite nicely on a my p133 with ET6000 PCI card.
  20.  
  21.         As you may notice, text mode is not restored.
  22.  
  23.         STAR.* include keyboard checking and is slow.
  24.         STARNOKB.*, removes the keyboard check. It runs much more smoothly.
  25.  
  26.  
  27. Code notes:
  28.  
  29.         I compile this code with Borland Turbo Assembler (TASM) 4.0
  30.         using the following commands:
  31.  
  32.         tasm star
  33.         tlink /t star
  34.  
  35.         to produce star.com. Starnokb.asm may be compiled the same way.
  36.  
  37.         The algorithm used is very small, albeit slow. An explanation
  38.         follows:
  39.  
  40.                               -Setup Code-
  41.         Some registers are assumed to be 0 on program entry (per DOS
  42. specification). First, graphics mode 13h is set by calling BIOS interrupt
  43. 10h with AH=0 (video mode set routine) and AL=13h (320x200x256). This
  44. code is unavoidable and cannot be optimized. BX is 0. Setting the high
  45. byte to a0 sets BX = A000, the segment of video memory. This is stored in
  46. ES. (This code is the same size as push 0a000h / pop es). An optional
  47. 'dec ch' will wrap ch from 00 to FFh, creating a very large number of stars.
  48.                          -Random Star Generator-
  49. This routine not only generates stars but also draws them on the screen.
  50. AL is loaded with a value from port 40h, the timer, for randomness.
  51. The AAA instruction converts the value in AL for specific reasons: The
  52. value from port 40h can vary widely, however, the display of stars looks
  53. ugly when there are too many random colors. In addition, with a large
  54. scroll distance/frame (associated with color), a star doesn't really
  55. appear to scroll; it appears to jerk around randomly. AAA takes care of
  56. this problem by filtering the random value from port 40h. AX (or AL,
  57. since AH = 0) is subtracted from DI providing a new location on the display
  58. for each pixel. The pixel color (AL), is stored at ES:[DI] with STOSB.
  59. LOOP GENSTAR repeats the process CX times. After the random star generation
  60. is finished, execution will procede through the loop statement.
  61.                              -Star Scrolling-
  62.         The next section of code performs animation. First, AX is cleared.
  63. XCHG AL,ES:[DI] does two things, erases the pixel on the screen (sets it
  64. equal to AL, which is 0 after the clear) and loads the pixel value in AL.
  65. CX is incremented (set to 1) so that execution will simply pass through
  66. LOOP GENSTAR. This allows the code after SCROLL: to serve double duty as
  67. part of both the randomizing and animation routines. Again, DI is
  68. decremented, moving the pixel to the left. The pixel is replotted with
  69. STOSB. LOOP decreases the value in CX, then compares it to 0. Since we
  70. set CX to 1, it will never loop to the first routine. The keyboard-checking
  71. version sets AH to 1 and calls interrupt 16H, checking for an available
  72. keystroke. (Note that this code was NOT optimized) If there is a keystroke
  73. (based on Zero flag), the byte at hidden_ret + 1 is executed. This is a RET
  74. instruction encoded by MOV ES,AX (a small optimization).
  75. The scrolling routine essentially reads a pixel, erases it, replots it at
  76. a position to the left equal to its color value. STOSB, used to plot, also
  77. increments DI. The pixel one unit to the right of the newly displaced pixel
  78. is read. Its value is 0 if blank. 0 loaded to AL, erased on the screen
  79. (set to 0 again), subtracted from DI and stored again. When a 0 is
  80. encountered, it passes through the engine as any other pixel, however, the
  81. only net result is the incrementation of DI. In this manner, the algorithm
  82. goes through video memory searching for pixels.
  83.         When a pixel goes off the left side of the screen, it reappears
  84. on the right side one column above. Subtracting a value from DI, after
  85. all, will never produce a value outside the 64k area allocated to video
  86. memory. Normally, it will just move the pixel left. In this case
  87. it wraps around the side. Similarly, when a pixel scrolls off the first
  88. line, it will reappear near the end of video memory. Since 2^16 = 65536
  89. and 320*200 = 64000, there are 1536 bytes of buffer that are not
  90. displayed at the end of the screen. The star moves along in this hidden
  91. area for a bit before becoming visible around DI=64000.
  92.  
  93.         To my knowledge, there are no more optimizations that can be
  94. performed without sacrificing quality.
  95.  
  96.         The SBB instructions are interchangeable for SUB. SBB just
  97. looks cooler :)
  98.  
  99.         When a star passes a slower one, an interesting
  100. effect occurs: a star will appear to leap. The animation routine
  101. traces through video memory from the point of the last star write.
  102. A fast star will be relocated behind a slower star that has already
  103. been processed in the iteration. The slower star will be processed
  104. again when the routine traces through memory to it. The slower star
  105. effectively moves twice as far as it normally would. I leave it as
  106. an exercise for the reader to explain this effect in astrophysical
  107. terms :)
  108.  
  109.         Stars disappear after a while. Disappearing occurs when a
  110. star jumps on top of another one. Since the only information about
  111. a star is itself (the pixel's color), overwriting this value
  112. deletes the star entirely.
  113.  
  114.  
  115. Modifications:
  116.  
  117.         One day, BigCheese of #coders (Adam Letts) came to me and said
  118. he had lopped off another byte from my program. The xchg instruction
  119. used to erase the pixel was his idea. Coupled with the support code
  120. (xor ax,ax) it was a byte smaller than my original entry (27 bytes
  121. opposed to 28). I had previously thought that there was no more
  122. optimization possible on the code. How wrong an assumption that was :)
  123. Vowing that it could be made smaller, we both shrunk the code an
  124. additional 3 bytes to its current 24 bytes (working independently of
  125. each other). It is possible to reduce it even further (see code
  126. comments) but that involves sacrifices in quality. Adam suggested
  127. the substitution of 'in al,40h' with 'inc ax.' This saves a byte but
  128. the star positioning is no longer random. He also suggested that I
  129. try to make use of the overlapping code in the generate/draw routines
  130. (sbb di,ax/stosb) which I had previously been too lazy to do :)
  131.  
  132.  
  133. Critical Acclaim:
  134.  
  135.    Chris Hargrove, Programmer at Raven Software
  136.         <kd> At some point, size optimization ceases to be optimization
  137.         and becomes compression. :)
  138.  
  139.    ThaDragon, #coders channel
  140.         <ThaDragon> os\2: fuck 3d starfields can prolly be made smaller
  141.         than that ;}
  142.  
  143.    Pascal of Cubic Team, CubicPlayer guy
  144.         <pascal> strange.
  145.         <pascal> very strange.
  146.         <pascal> your program is too strange..
  147.  
  148.    Moopy, #coders channel
  149.         <Moopy> could you sent it to lewpy
  150.  
  151.    Lewpy, #coders channel
  152.         <Lewpy> Stumpy Oleg McNoleg
  153.  
  154.    BigCheese, #coders channel
  155.         <BigCheese> os2man: it's crap, but hey :)
  156.         <BigCheese> os2man: Why don't you follow the standard and call it
  157.         tinystar.com or something? :)
  158.  
  159.  
  160. About me (blab):
  161.         My name is Matt Wilhelm. I am an 18 year old student about
  162. to enter freshman year at the California Institute of Technology
  163. (CIT, Caltech). My interests include computer graphics, algorithm
  164. optimizations, Intel assembly code tweaking, and structured/OOP
  165. programming in C++. My current project is a graphics engine for
  166. Microsoft DirectX (c) (TM) (whatever) that features realtime lossless
  167. image decompression, color conversion between various hi/trucolor
  168. formats, dynamic memory management (NOT as in Windows swapping), and
  169. many other useful features. I may or may not finish and release this
  170. C++/ASM library depending on how much programming time I have once
  171. school begins. I have also become interested in non-polygon 3D methods
  172. including Splines, NURBS, and fast raytracing. I may work on modeling
  173. worlds with mechanical and visual correctness.
  174.  
  175.         Until 9/20/96, I may be mailed at:
  176.         FDWilhelm@worldnet.att.net
  177.  
  178. _PLEASE_DO_NOT_ use this address after that date. I will start Caltech
  179. the following day and have a nice, fast ethernet connection. I do not
  180. know what my e-mail address will be.
  181.  
  182. I am often in the IRC #coders channel on the European side of EFNet.
  183. To connect to this network from inside the US, try the server
  184. irc-2.stealth.net(:6667). My IRC nick is os\2man.
  185.